package com.orkasgb.framework.hadoop.mapperreduce.combiner;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class WordCountDriver {

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

        final Configuration configuration = new Configuration();
        // 设置wordcount计算时是否忽略大小写
        configuration.setBoolean("wordcount.case.sensitive", true);

        Job job = Job.getInstance(configuration);

        // 设置job名称
        job.setJobName("WordCount");

        job.setJarByClass(WordCountDriver.class);

        // 指定Mapper和Reduce
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReduce.class);

        // 设置Mapper输出K/V类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        // 推荐做法，直接将Reduce设置为Combiner
        // job.setCombinerClass(WordCountReduce.class);
        job.setCombinerClass(WordCountCombiner.class);

        // 设置最终输出K/V类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        // 指定输入输出文件
        FileInputFormat.setInputPaths(job, new Path("F:\\hadoop\\input\\combiner\\combiner.txt"));
        FileOutputFormat.setOutputPath(job, new Path("F:\\hadoop\\output\\combiner\\combiner2"));

        final boolean wait = job.waitForCompletion(true);
        System.exit(wait ? 0 : 1);
    }
}
